﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Routing;

namespace StockMatch.WebSite.Mvc
{
    public static class RouteCollectionExtensions
    {
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name)
        {
            return routes.MapDomainRoute(name, null);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, object defaults)
        {
            return routes.MapDomainRoute(name, defaults, null);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, string[] namespaces)
        {
            return routes.MapDomainRoute(name, null, namespaces);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, object defaults, object constraints)
        {
            DomainRouteSection mainSite = DomainConfig.GetDomainRouteConfig(name);
            return routes.MapDomainRoute(name, mainSite.domain, mainSite.url, defaults, constraints, null);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, object defaults, string[] namespaces)
        {
            DomainRouteSection mainSite = DomainConfig.GetDomainRouteConfig(name);
            return routes.MapDomainRoute(name, mainSite.domain, mainSite.url, defaults, null, namespaces);
        }


        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, string domain, string url)
        {
            return routes.MapDomainRoute(name, domain, url, null, null);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, string domain, string url, object defaults)
        {
            return routes.MapDomainRoute(name, domain, url, defaults, null);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, string domain, string url, string[] namespaces)
        {
            return routes.MapDomainRoute(name, domain, url, null, null, namespaces);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, string domain, string url, object defaults, object constraints)
        {
            return routes.MapDomainRoute(name, domain, url, defaults, constraints, null);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, string domain, string url, object defaults, string[] namespaces)
        {
            return routes.MapDomainRoute(name, domain, url, defaults, null, namespaces);
        }
        public static DomainRoute MapDomainRoute(this RouteCollection routes, string name, string domain, string url, object defaults, object constraints, string[] namespaces)
        {
            if (routes == null)
            {
                throw new ArgumentNullException("routes");
            }
            if (url == null)
            {
                throw new ArgumentNullException("url");
            }
            var item = new DomainRoute(domain, url, new MvcRouteHandler())
            {
                Defaults = new RouteValueDictionary(defaults),
                Constraints = new RouteValueDictionary(constraints),
                DataTokens = new RouteValueDictionary()
            };
            if ((namespaces != null) && (namespaces.Length > 0))
            {
                item.DataTokens["Namespaces"] = namespaces;
                item.DataTokens["UseNamespaceFallback"] = false;
            }
            //bool flag = (namespaces == null) || (namespaces.Length == 0);
            //item.DataTokens["UseNamespaceFallback"] = flag;
            if (!string.IsNullOrEmpty(domain))
            {
                item.DataTokens["domain"] = domain;
            }
            routes.Add(name, item);
            return item;
        }

    }
}
